package org.fastiot.protocol.codec;

import java.text.DecimalFormat;

public class BytesUtils {
    public static byte[] convertHexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        char[] schar = s.toCharArray();
        for (int i = 0; i < len; i += 2) {
            data[(int)(i / 2)] = (byte)((Integer.parseInt(Character.toString(schar[i]), 16) << 4) + Integer.parseInt(Character.toString(schar[i + 1]), 16));
        }
        return data;
    }

    public static byte[] convertIntegerToByteArray(int i) {
        byte[] result = new byte[4];

        result[0] = (byte) (i >> 24);
        result[1] = (byte) (i >> 16);
        result[2] = (byte) (i >> 8);
        result[3] = (byte) (i /*>> 0*/);

        return result;
    }

    public static String convertByteArrayToCompressedBCD(byte[] byteArrays) {
        StringBuilder temp = new StringBuilder(byteArrays.length * 2);
        for (int i = 0; i < byteArrays.length; i++) {
            temp.append((byte) ((byteArrays[i] & 0xf0) >>> 4));
            temp.append((byte) (byteArrays[i] & 0x0f));
        }
        return temp.toString();
    }

    public static String convertByteArrayToUncompressedBCD(byte[] byteArrays) {
        StringBuilder temp = new StringBuilder(byteArrays.length * 2);
        for (int i = 0; i < byteArrays.length; i++) {
            temp.append((byte) ((byteArrays[i] & 0xf0) >>> 4));
            temp.append((byte) (byteArrays[i] & 0x0f));
        }
        return temp.toString();
    }

    public static byte[] convertCompressedBCD2ByteArray(String bcd) {
        int len = bcd.length();
        int mod = len % 2;

        if (mod != 0) {
            bcd = "0" + bcd;
            len = bcd.length();
        }

        byte abt[] = new byte[len];
        if (len >= 2) {
            len = len / 2;
        }

        byte bbt[] = new byte[len];
        abt = bcd.getBytes();
        int j, k;

        for (int p = 0; p < bcd.length() / 2; p++) {
            if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
                j = abt[2 * p] - '0';
            } else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
                j = abt[2 * p] - 'a' + 0x0a;
            } else {
                j = abt[2 * p] - 'A' + 0x0a;
            }

            if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
                k = abt[2 * p + 1] - '0';
            } else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
                k = abt[2 * p + 1] - 'a' + 0x0a;
            } else {
                k = abt[2 * p + 1] - 'A' + 0x0a;
            }

            int a = (j << 4) + k;
            byte b = (byte) a;
            bbt[p] = b;
        }

        return bbt;
    }

    public static byte[] convertUncompressedBCD2ByteArray(String bcd) {
        throw new UnsupportedOperationException("");
    }

    public static String prettySize(long size) {
        if(size <= 0) return "0";
        final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
        int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
    }
}
